/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ResultadosAlumnoFrame.java
 *
 * Created on 10-ene-2012, 11:45:38
 */
package occho.view;

import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreeSelectionModel;
import occho.model.Asignatura;
import occho.model.Test;
import occho.model.Usuario;
import occho.view.resources.Messages;

/**
 *
 * @author Jorge
 */
public class ResultadosAlumnoFrame extends javax.swing.JInternalFrame implements Internacionalizable {
    
    private Usuario alumno = null;
    private Asignatura asignatura = null;
    private Test test = null;

    /** Creates new form ResultadosAlumnoFrame
     * @param alumno 
     */
    public ResultadosAlumnoFrame(Usuario alumno) {
        initComponents();
        
        this.alumno = alumno;
        setLenguaje();
        cargarArbol();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jSplitPane1 = new javax.swing.JSplitPane();
        treePanel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        asignaturasTree = new javax.swing.JTree();
        infoPanel = new javax.swing.JPanel();
        asignaturaPanel = new javax.swing.JPanel();
        txtNombreAsignatura = new javax.swing.JTextField();
        txtNotaMedia = new javax.swing.JTextField();
        lbNombreAsignatura = new javax.swing.JLabel();
        lbNotaMedia = new javax.swing.JLabel();
        testPanel = new javax.swing.JPanel();
        txtNombreTest = new javax.swing.JTextField();
        lbNombreTest = new javax.swing.JLabel();
        txtNotaTest = new javax.swing.JTextField();
        lbNotaTest = new javax.swing.JLabel();
        txtAciertos = new javax.swing.JTextField();
        lbAciertos = new javax.swing.JLabel();

        setClosable(true);
        setName("Form"); // NOI18N

        jSplitPane1.setDividerLocation(200);
        jSplitPane1.setName("jSplitPane1"); // NOI18N

        treePanel.setName("treePanel"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        javax.swing.tree.DefaultMutableTreeNode treeNode1 = new javax.swing.tree.DefaultMutableTreeNode("JTree");
        javax.swing.tree.DefaultMutableTreeNode treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("colors");
        javax.swing.tree.DefaultMutableTreeNode treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("blue");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("violet");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("red");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("yellow");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("sports");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("basketball");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("soccer");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("football");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("hockey");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("tennis");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        treeNode2 = new javax.swing.tree.DefaultMutableTreeNode("food");
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("hot dogs");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("pizza");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("ravioli");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("bananas");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("potatoes");
        treeNode2.add(treeNode3);
        treeNode3 = new javax.swing.tree.DefaultMutableTreeNode("oranges");
        treeNode2.add(treeNode3);
        treeNode1.add(treeNode2);
        asignaturasTree.setModel(new javax.swing.tree.DefaultTreeModel(treeNode1));
        asignaturasTree.setName("asignaturasTree"); // NOI18N
        asignaturasTree.addTreeSelectionListener(new javax.swing.event.TreeSelectionListener() {
            public void valueChanged(javax.swing.event.TreeSelectionEvent evt) {
                asignaturasTreeValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(asignaturasTree);

        javax.swing.GroupLayout treePanelLayout = new javax.swing.GroupLayout(treePanel);
        treePanel.setLayout(treePanelLayout);
        treePanelLayout.setHorizontalGroup(
            treePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 199, Short.MAX_VALUE)
        );
        treePanelLayout.setVerticalGroup(
            treePanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
        );

        jSplitPane1.setLeftComponent(treePanel);

        infoPanel.setName("infoPanel"); // NOI18N
        infoPanel.setLayout(new javax.swing.BoxLayout(infoPanel, javax.swing.BoxLayout.Y_AXIS));

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(occho.OcchoApp.class).getContext().getResourceMap(ResultadosAlumnoFrame.class);
        asignaturaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("asignaturaPanel.border.title"))); // NOI18N
        asignaturaPanel.setName("asignaturaPanel"); // NOI18N
        asignaturaPanel.setPreferredSize(new java.awt.Dimension(517, 100));

        txtNombreAsignatura.setEditable(false);
        txtNombreAsignatura.setText(resourceMap.getString("txtNombreAsignatura.text")); // NOI18N
        txtNombreAsignatura.setName("txtNombreAsignatura"); // NOI18N

        txtNotaMedia.setEditable(false);
        txtNotaMedia.setText(resourceMap.getString("txtNotaMedia.text")); // NOI18N
        txtNotaMedia.setName("txtNotaMedia"); // NOI18N

        lbNombreAsignatura.setText(resourceMap.getString("lbNombreAsignatura.text")); // NOI18N
        lbNombreAsignatura.setName("lbNombreAsignatura"); // NOI18N

        lbNotaMedia.setText(resourceMap.getString("lbNotaMedia.text")); // NOI18N
        lbNotaMedia.setName("lbNotaMedia"); // NOI18N

        javax.swing.GroupLayout asignaturaPanelLayout = new javax.swing.GroupLayout(asignaturaPanel);
        asignaturaPanel.setLayout(asignaturaPanelLayout);
        asignaturaPanelLayout.setHorizontalGroup(
            asignaturaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(asignaturaPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(asignaturaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbNotaMedia)
                    .addComponent(lbNombreAsignatura))
                .addGap(18, 18, 18)
                .addGroup(asignaturaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNotaMedia, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        asignaturaPanelLayout.setVerticalGroup(
            asignaturaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(asignaturaPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(asignaturaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbNombreAsignatura)
                    .addComponent(txtNombreAsignatura, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(asignaturaPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbNotaMedia)
                    .addComponent(txtNotaMedia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(16, Short.MAX_VALUE))
        );

        infoPanel.add(asignaturaPanel);

        testPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(resourceMap.getString("testPanel.border.title"))); // NOI18N
        testPanel.setName("testPanel"); // NOI18N

        txtNombreTest.setEditable(false);
        txtNombreTest.setText(resourceMap.getString("txtNombreTest.text")); // NOI18N
        txtNombreTest.setName("txtNombreTest"); // NOI18N

        lbNombreTest.setText(resourceMap.getString("lbNombreTest.text")); // NOI18N
        lbNombreTest.setName("lbNombreTest"); // NOI18N

        txtNotaTest.setEditable(false);
        txtNotaTest.setText(resourceMap.getString("txtNotaTest.text")); // NOI18N
        txtNotaTest.setName("txtNotaTest"); // NOI18N

        lbNotaTest.setText(resourceMap.getString("lbNotaTest.text")); // NOI18N
        lbNotaTest.setName("lbNotaTest"); // NOI18N

        txtAciertos.setEditable(false);
        txtAciertos.setText(resourceMap.getString("txtAciertos.text")); // NOI18N
        txtAciertos.setName("txtAciertos"); // NOI18N

        lbAciertos.setText(resourceMap.getString("lbAciertos.text")); // NOI18N
        lbAciertos.setName("lbAciertos"); // NOI18N

        javax.swing.GroupLayout testPanelLayout = new javax.swing.GroupLayout(testPanel);
        testPanel.setLayout(testPanelLayout);
        testPanelLayout.setHorizontalGroup(
            testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(testPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbAciertos)
                    .addComponent(lbNombreTest)
                    .addComponent(lbNotaTest))
                .addGap(32, 32, 32)
                .addGroup(testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtNotaTest, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtNombreTest, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtAciertos, javax.swing.GroupLayout.PREFERRED_SIZE, 220, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        testPanelLayout.setVerticalGroup(
            testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(testPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbNombreTest)
                    .addComponent(txtNombreTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbNotaTest)
                    .addComponent(txtNotaTest, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(testPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lbAciertos)
                    .addComponent(txtAciertos, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(19, Short.MAX_VALUE))
        );

        infoPanel.add(testPanel);

        jSplitPane1.setRightComponent(infoPanel);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 527, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jSplitPane1, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 239, Short.MAX_VALUE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void asignaturasTreeValueChanged(javax.swing.event.TreeSelectionEvent evt) {//GEN-FIRST:event_asignaturasTreeValueChanged
    DefaultMutableTreeNode selectedNode = 
            (DefaultMutableTreeNode) asignaturasTree.getLastSelectedPathComponent();
    switch (selectedNode.getLevel()) {
        case 1:
            asignatura = (Asignatura) selectedNode.getUserObject();
            test = null;
            break;
        case 2:
            test = (Test) selectedNode.getUserObject();
            asignatura = (Asignatura) ((DefaultMutableTreeNode) selectedNode.getParent()).getUserObject();
            break;
        default:
            test = null;
            asignatura = null;
            break;
    }
    mostrarDatos();
}//GEN-LAST:event_asignaturasTreeValueChanged

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JPanel asignaturaPanel;
    private javax.swing.JTree asignaturasTree;
    private javax.swing.JPanel infoPanel;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSplitPane jSplitPane1;
    private javax.swing.JLabel lbAciertos;
    private javax.swing.JLabel lbNombreAsignatura;
    private javax.swing.JLabel lbNombreTest;
    private javax.swing.JLabel lbNotaMedia;
    private javax.swing.JLabel lbNotaTest;
    private javax.swing.JPanel testPanel;
    private javax.swing.JPanel treePanel;
    private javax.swing.JTextField txtAciertos;
    private javax.swing.JTextField txtNombreAsignatura;
    private javax.swing.JTextField txtNombreTest;
    private javax.swing.JTextField txtNotaMedia;
    private javax.swing.JTextField txtNotaTest;
    // End of variables declaration//GEN-END:variables
    
    private void cargarArbol() {
        DefaultMutableTreeNode top =new DefaultMutableTreeNode("Asignaturas");
        createNodes(top);
        DefaultTreeModel modelo = new DefaultTreeModel(top);
        
        this.asignaturasTree.setModel(modelo);
        this.asignaturasTree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
    }
    
    private void createNodes(DefaultMutableTreeNode top) {
        DefaultMutableTreeNode asignaturaNode = null;
        DefaultMutableTreeNode testNode = null;
        
        for (Asignatura asig : alumno.getAsignaturas()) {
            asignaturaNode = new DefaultMutableTreeNode(asig);
            top.add(asignaturaNode);
            
            for (Test t : asig.getTests()) {
                testNode = new DefaultMutableTreeNode(t);
                asignaturaNode.add(testNode);
            }
        }
    }
    
    private void mostrarDatos() {
        if (asignatura != null) {
            NumberFormat format = Messages.getNumberFormat();
            txtNombreAsignatura.setText(asignatura.getNombre());
            txtNotaMedia.setText(format.format(asignatura.getNotaMedia()));
            
            if (test != null) {
                txtNombreTest.setText(test.getNombre());
                txtNotaTest.setText(format.format(test.getNota()));
                txtAciertos.setText(test.getAciertos() + " / " + test.getNumPreguntas());
            } else {
                txtNombreTest.setText("");
                txtNotaTest.setText("");
                txtAciertos.setText("");
            }
        } else {
            txtNombreAsignatura.setText("");
            txtNotaMedia.setText("");
            txtNombreTest.setText("");
            txtNotaTest.setText("");
            txtAciertos.setText("");
        }
    }

    /**
     * Establece lenguaje
     */
    public void setLenguaje() {
        
        lbNombreAsignatura.setText(Messages.getString("lbNombreAsignatura.text", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO));
        asignaturaPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Messages.getString("asignaturaPanel.border.title", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO)));
        lbNotaMedia.setText(Messages.getString("lbNotaMedia.text", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO));
        testPanel.setBorder(javax.swing.BorderFactory.createTitledBorder(null, Messages.getString("testPanel.border.title", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO)));
        lbNotaTest.setText(Messages.getString("lbNotaTest.text", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO));
        lbNombreTest.setText(Messages.getString("lbNombreTest.text", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO));
        lbAciertos.setText(Messages.getString("lbAciertos.text", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO));
        setTitle(Messages.getString("Form.title", Messages.BUNDLE_NAME_RESULTADOS_ALUMNO));        
        
        
    }
}
